package com.sds.system.helpers;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

public class ResourceUtils {

    private static Logger LOGGER = Logger.getLogger(ResourceUtils.class.getName());

    public static List<String> readLinesIgnoringComment(InputStream stream) {
        return removeComment(readLines(stream));
    }

    public static List<String> readLines(InputStream stream) {
        try {
            return IOUtils.readLines(stream, "UTF-8");
        } catch (IOException e) {
            LOGGER.warning("Unexpected exception: " + e + "[" + e.getLocalizedMessage() + "]");
            return Lists.newArrayList();
        }
    }

    public static List<String> removeComment(List<String> lines) {
        return Lists.newArrayList(Iterables.filter(lines, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return !org.apache.commons.lang.StringUtils.isEmpty(input) && !input.trim().startsWith("#");
            }
        }));
    }
}
